# 62. 多段线数据压缩

62 62-1

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
const points = [];
rl.on('line', function(line) {
    const nums = line.split(' ').map(Number);
    for(let i=0; i<nums.length; i+=2) {
        points.push([nums[i], nums[i+1]]);
    }
});
rl.on('close', () => {
    const simplePoints = simplePath(points);
    const output = simplePoints.map(x => x.join(' ')).join(' ');
    console.log(output);
})
function simplePath(points) {
    if (points.length < 2) return points;
    const res = [points[0]];
    for(let i=1; i<points.length-1; i++) {
        if (isGuaiDian(points[i-1], points[i], points[i+1])) {
            res.push(points[i]);
        }
    }

    res.push(points[points.length-1]);
    return res;
}
function isGuaiDian(prev, curr, next) {
    // 计算向量
    const dx1 = curr[0] - prev[0];
    const dx2 = next[0] - curr[0];
    const dy1 = curr[1] - prev[1];
    const dy2 = next[1] - curr[1];

    return dx1 * dy2 !== dx2 * dy1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38